package com.wanpinghui.supplier.user;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.adapter.SelectCityAdapter;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.model.user.bean.AreaBean;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * 功能描述:选择城市
 * author:qiujialiu
 * time:2017/11/23
 */
@ContentView(R.layout.layout_activity_select_city)
public class SelectCityActivity extends BaseActivity {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;

    @ViewInject(R.id.expandableListView)
    private ExpandableListView expandableListView;

    private SelectCityAdapter mAdapter;


    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        List<AreaBean> areaBeanList = new Gson().fromJson(getJson(SelectCityActivity.this, "prov_city.json"), new TypeToken<List<AreaBean>>() {
        }.getType());
        if (areaBeanList != null) {
            mAdapter = new SelectCityAdapter(areaBeanList, SelectCityActivity.this);
            expandableListView.setGroupIndicator(null);
            expandableListView.setDivider(null);
            expandableListView.setAdapter(mAdapter);
        }

    }

    @Override
    public void bindEvents() {
        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                AreaBean areaBean = mAdapter.getData(groupPosition, childPosition);
                if (areaBean != null) {
                    Intent intent = new Intent();
                    intent.putExtra("city", areaBean.getName());
                    setResult(RESULT_OK, intent);
                }
                finish();
                return true;
            }
        });
    }

    @Override
    public void initData() {

    }

    private String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
